//
//  Crate.m
//  SSE
//
//  Created by Matt Roman on 10/11/07.
//  Copyright 2007 OU. All rights reserved.
//

#import "Crate.h"

// cube ///////////////////////////////////////////////////////////////////////
//		 Z
//       |
//		 |       
//    v7----- v6 
//   /|      /|
//  v4------v5|	RIGHT
//  | |     | |
//  | |v1---|-|v2
//  |/   .  |/ -------> X
//  v0------v3
//	  FRONT

@implementation Crate

- (id)initCrateAlpha:(GLfloat)angle 
				   X:(GLfloat)x 
				   Y:(GLfloat)y 
				   Z:(GLfloat)z 
				   L:(float)l
				   W:(float)w
				   H:(float)h
{
	float hyp,gamma;
	Vector3D tempV1,tempV2;
	
	[super init];
	alpha = angle;
	position.x = x;
	position.y = y;
	position.z = z;
	color[1] = 1.0;
	color[3] = 0.8;
	length = l;
	width = w;
	height = h;
	
	hyp = sqrt(l*l+w*w)/2.0;
	gamma = atan((w/2.0)/(l/2.0));
	
	// bottom
	corners[0].x=(float)position.x - hyp*cos(gamma + alpha);		// v0
	corners[0].y=(float)position.y - hyp*sin(gamma + alpha);
	corners[0].z=position.z;
	
	corners[1].x=(float)position.x + hyp*cos(PI-gamma+alpha);		// v1
	corners[1].y=(float)position.y + hyp*sin(PI-gamma+alpha);
	corners[1].z=position.z;
	
	corners[2].x=(float)position.x + hyp*cos(gamma + alpha);		// v2
	corners[2].y=(float)position.y + hyp*sin(gamma + alpha);
	corners[2].z=position.z;
	
	corners[3].x= (float)position.x - hyp*cos(PI-gamma+alpha);		// v3	
	corners[3].y= (float)position.y - hyp*sin(PI-gamma+alpha);
	corners[3].z= position.z;
	
	pointsToVector(corners[0], corners[1], &tempV1);
	pointsToVector(corners[0], corners[3], &tempV2);
	NormalizeCross(&tempV1, &tempV2, &norms[0]);
	
	// top
	corners[4] = corners[0];		// v4
	corners[4].z += h;
	corners[5] = corners[3];		// v5
	corners[5].z += h;
	corners[6] = corners[2];		// v6
	corners[6].z += h;
	corners[7] = corners[1];		// v7
	corners[7].z += h;
	
	pointsToVector(corners[4], corners[5], &tempV1);
	pointsToVector(corners[4], corners[7], &tempV2);
	NormalizeCross(&tempV1, &tempV2, &norms[1]);
	
	// LEFT
	corners[8] = corners[0];		//	v0
	corners[9] = corners[4];		//	v4	
	corners[10] = corners[7];		//	v7
	corners[11] = corners[1];		//	v1
	
	pointsToVector(corners[8], corners[9], &tempV1);
	pointsToVector(corners[8], corners[11], &tempV2);
	NormalizeCross(&tempV1, &tempV2, &norms[2]);
	
	// RIGHT
	corners[12] = corners[3];		//	v3
	corners[13] = corners[2];		//	v2
	corners[14] = corners[6];		//	v6
	corners[15] = corners[5];		//	v5
	
	pointsToVector(corners[12], corners[13], &tempV1);
	pointsToVector(corners[12], corners[15], &tempV2);
	NormalizeCross(&tempV1, &tempV2, &norms[3]);
	
	// FRONT
	corners[16] = corners[0];	//	v0
	corners[17] = corners[3];	//	v3
	corners[18] = corners[5];	//	v5
	corners[19] = corners[4];	//	v4
	
	pointsToVector(corners[16], corners[17], &tempV1);
	pointsToVector(corners[16], corners[19], &tempV2);
	NormalizeCross(&tempV1, &tempV2, &norms[4]);
	
	// BACK
	corners[20] = corners[2];		//	v2
	corners[21] = corners[1];		//	v1
	corners[22] = corners[7];		//	v7
	corners[23] = corners[6];		//	v6
	
	pointsToVector(corners[20], corners[21], &tempV1);
	pointsToVector(corners[20], corners[23], &tempV2);
	NormalizeCross(&tempV1, &tempV2, &norms[5]);
	return self;
}

- (void)drawCrate
{
	int i,j=-1;
	
	glColor4fv(color);
	glBegin(GL_QUADS);
	for(i=0;i<24;i++)
	{		
		if(i%4 == 0) j++;
		glNormal3f(norms[j].x, norms[j].y, norms[j].z);
		glVertex3f(corners[i].x, corners[i].y, corners[i].z);
	}
	glEnd();
	///////////////////////////////////////////////////////////////////
	// DRAWS A POINT AT THE CENTER OF THE OBSTACLE
	//glPointSize(5.0);
	//glBegin(GL_POINTS);
	//	glColor3f(1,1,1);
	//	glVertex3f(corners[0].x,corners[0].y,corners[0].z);
	//glEnd();
}

- (void)setColorR:(GLfloat)r G:(GLfloat)g B:(GLfloat)b A:(GLfloat)a
{
	color[0] = r;
	color[1] = g;
	color[2] = b;
	color[3] = a;
}

- (Point3D)getPosition
{
	return position;
}

- (float)getLength
{
	return length;
}

- (float)getWidth
{
	return width;
}

- (float)getAlpha
{
	return alpha;
}

- (Point3D *)getCorners
{
	return corners;
}
- (Vector3D *)getNorms
{
	return norms;
}
@end
